package cn.hfatec.shms.system.resource.action;

import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.base.ShmsBaseAction;
import cn.hfatec.shms.system.module.model.Module;
import cn.hfatec.shms.system.module.service.ModuleService;
import cn.hfatec.shms.system.resource.model.ResourceParams;
import cn.hfatec.shms.system.resource.model.ResourceView;
import cn.hfatec.shms.system.resource.model.Resources;
import cn.hfatec.shms.system.resource.service.ResourceService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author hfatec codegen
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Controller
@RequestMapping("/system/resource")
public class ResourceAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "ResourceService")
    private ResourceService resourceService;

    @Resource(name = "ModuleService")
    private ModuleService moduleService;


    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(ResourceParams resourceParams, Page page, Model model) {

        Table table = resourceService.queryResourceTable(resourceParams, page);
        model.addAttribute("resourcesTable", table);
        model.addAttribute("resourceParams", resourceParams);
        return "/system/resource/list";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(ResourceParams resourceParams, Page page, Model model) {
        try {
            list(resourceParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/system/resource/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到新增页
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(String moduleId,String authId, Model model) {
        try {
            ResourceView resource = new ResourceView();
            Module module = moduleService.queryModuleByModuleId(moduleId);
            resource.setModuleId(module.getModuleId());
            resource.setModuleName(module.getModuleName());
            model.addAttribute("authId", authId);
            model.addAttribute("resource", resource);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/system/resource/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            ResourceView resource = resourceService.getResource(id);
            model.addAttribute("resource", resource);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/system/resource/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 根据IDs删除数据
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteResource(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if (ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            resourceService.deleteResourceBatch(idList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 保存数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveResource(@Valid Resources resource, @RequestParam("authId") String authId, BindingResult result) {

        try {
            if (result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            resourceService.saveResource(resource, authId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据Resource保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }
}
